/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import entities.Spot;
import entities.User;
import java.awt.event.ActionEvent;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpSession;
import org.primefaces.context.RequestContext;

/**
 *
 * @author HP
 */
@ManagedBean(name = "loginBean")
@SessionScoped
public class LoginBean {

    @PersistenceContext 
    private EntityManager em;
    private User user;
    private boolean loggedIn = false;
    
    public LoginBean() {
        this.user = new User();
    }
      
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getUsername() {
        return this.user.getUsername();
    }
    public boolean isLoggedIn() {
        return loggedIn;
    }

    public void setLoggedIn(boolean loggedIn) {
        this.loggedIn = loggedIn;
    }

    public EntityManager getEm() {
        return em;
    }

    public void setEm(EntityManager em) {
        this.em = em;
    }
    
    public void login(ActionEvent actionEvent) {
        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;
        loggedIn = false;
        User foundUser = searchForUser();
        if (foundUser == null) {
            loggedIn = false;
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "Invalid credentials");
        } else if (!foundUser.getPassword().equals(user.getPassword())) {
            loggedIn = false;
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "Invalid credentials");

        } else {
            loggedIn = true;
            user = foundUser;
            HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true);
            session.setAttribute("user", user);
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", user.getUsername());
        }
//		if(username != null  &&&& username.equals("admin") && password != null  && password.equals("admin")) {
//			loggedIn = true;
//			msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", username);
//		} else {
//			loggedIn = false;
//			msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "Invalid credentials");
//		}

        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("loggedIn", loggedIn);
    }

    public void logout(ActionEvent actionEvent) {
        RequestContext context = RequestContext.getCurrentInstance();

        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Bye Bye", user.getUsername());

        FacesContext.getCurrentInstance().addMessage(null, msg);
        if (context != null) {
            context.release();
        }
        user = new User();
        loggedIn = false;
    }

     private User searchForUser() {
        try {
                return (User) em.createNamedQuery("User.findByUsername").setParameter("username", user.getUsername()).getSingleResult();
            } catch (NoResultException nre) {
                return null;
            }
    }
     
     public List<Spot> listSpots() {
        try {
            return (List<Spot>) em.createNamedQuery("Spot.findByUsername").setParameter("username", user).getResultList();
        } catch (NoResultException nre) {
            return null;
        }
    }
}
